package com.ruixi.spring.jdbcTemplete.mapper;

import com.ruixi.spring.jdbcTemplete.entity.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import sun.dc.pr.PRError;

import java.util.Arrays;
import java.util.List;

@Repository
public class BookMapperImpl implements BookMapper{

    //注入JdbcTemplate
    @Autowired
    private JdbcTemplate jdbcTemplate;

    //添加
    @Override
    public void add(Book book) {
        //SQL语句
        String sql = "INSERT INTO book VALUES(?,?,?)";
        //返回值为受影响行数
        int add = this.jdbcTemplate.update(sql, book.getId(), book.getName(), book.getStatus());
        System.out.println(add);
    }

    //修改
    @Override
    public void update(Book book) {
        //SQL语句
        String sql = "UPDATE book SET name = ?, status = ? WHERE id = ?";
        //返回值为受影响行数
        int update = this.jdbcTemplate.update(sql,book.getName(),book.getStatus(),book.getId());
        System.out.println(update);

    }

    //删除
    @Override
    public void delete(String id) {
        //SQL语句
        String sql = "DELETE FROM book WHERE id = ?";
        //返回值为受影响行数
        int delete = this.jdbcTemplate.update(sql,id);
        System.out.println(delete);
    }

    //查询 返回某个值
    @Override
    public Integer findCount() {
        //SQL语句
        String sql = "SELECT COUNT(*) FROM book";
        //第一个参数为SQL语句,第二个参数为返回类型class
        Integer integer = this.jdbcTemplate.queryForObject(sql,Integer.class);
        return integer;
    }

    //查询 返回对象
    @Override
    public Book findByID(String id) {
        //SQL语句
        String sql = "SELECT * FROM book WHERE id = ?";
        //第一个参数为SQL语句,第二个参数为RowMapper,是一个接口,针对返回不同类型数据,使用这个接口中的实现类完成数据的封装,第三个参数为查询条件
        Book book = this.jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<Book>(Book.class), id);
        return book;
    }

    //查询 返回集合
    @Override
    public List<Book> findAll() {
        //SQL语句
        String sql = "SELECT * FROM book";
        //第一个参数为SQL语句,第二个参数为RowMapper,是一个接口,针对返回不同类型数据,使用这个接口中的实现类完成数据的封装,第三个参数为查询条件
        List<Book> list = this.jdbcTemplate.query(sql, new BeanPropertyRowMapper<Book>(Book.class));
        return list;
    }

    //批量添加
    @Override
    public void saveBatch(List<Object[]> list) {
        //SQL语句
        String sql = "INSERT INTO book VALUES(?,?,?)";
        //返回值为受影响行数
        //第一个参数为SQL语句,第二个参数为添加的多条记录数据
        int[] ints = this.jdbcTemplate.batchUpdate(sql, list);
        System.out.println(Arrays.toString(ints));
    }

    //批量修改
    @Override
    public void updateBatch(List<Object[]> list) {
        //SQL语句
        String sql = "UPDATE book SET name = ?, status = ? WHERE id = ?";
        //返回值为受影响行数
        //第一个参数为SQL语句,第二个参数为修改的多条记录数据 每条记录的字段顺序需要和SQL中的占位符顺序相同
        int[] ints = this.jdbcTemplate.batchUpdate(sql, list);
        System.out.println(Arrays.toString(ints));
    }

    //批量删除
    @Override
    public void deleteBatch(List<Object[]> list) {
        //SQL语句
        String sql = "DELETE FROM book WHERE id = ?";
        int[] ints = this.jdbcTemplate.batchUpdate(sql, list);
        System.out.println(Arrays.toString(ints));
    }
}
